<?php

/**
 * blog actions.
 *
 * @package    probolnici
 * @subpackage blog
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class blogActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->posts = new sfDoctrinePager(
            'BlogPost', 
            sfConfig::get('app_max_posts_on_page')
            );
    $this->posts->setTableMethod('getAllPosts');
    $this->posts->setPage($request->getParameter('page', 1));
    $this->posts->init();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->post = Doctrine_Core::getTable('BlogPost')->getOnePost($request->getParameter('slug'));
    $this->forward404Unless($this->post);
    $this->form = new BlogCommentForm();
    
    $categoryUrl = $this->post->getCategoryUrl();
    $this->redirectIf($request->getParameter('category') != $categoryUrl, '@blog_show?category='.$categoryUrl.'&slug='.$this->post->getName());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->forward404();
    $this->form = new BlogPostForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404();
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BlogPostForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404();
    $this->forward404Unless($blog_post = Doctrine_Core::getTable('BlogPost')->findOneByName($request->getParameter('slug')), sprintf('Object blog_post does not exist (%s).', $request->getParameter('slug')));
    $this->form = new BlogPostForm($blog_post);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404();
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($blog_post = Doctrine_Core::getTable('BlogPost')->findOneByName($request->getParameter('slug')), sprintf('Object blog_post does not exist (%s).', $request->getParameter('slug')));
    $this->form = new BlogPostForm($blog_post);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404();
    $request->checkCSRFProtection();

    $this->forward404Unless($blog_post = Doctrine_Core::getTable('BlogPost')->find(array($request->getParameter('slug'))), sprintf('Object blog_post does not exist (%s).', $request->getParameter('slug')));
    $blog_post->delete();

    $this->redirect('blog/index');
  }
  
  public function executeRedirect()
  {
    $this->redirect('@blog', 301);
  }
  
  public function executeCategory(sfWebRequest $request) 
  {
    $this->posts = new sfDoctrinePager('BlogPost',
                                   sfConfig::get('app_max_posts_on_page'));
    
    $this->posts->setQuery(Doctrine_Core::getTable('BlogPost')->getOneCategory($request->getParameter('category')));
    
    $this->posts->setPage($request->getParameter('page', 1));
    $this->posts->init();
    
    $this->forward404Unless($this->posts->getNbResults() > 0);
    
    $this->category = Doctrine_Core::getTable('BlogCategory')
            ->findOneByUrl($request->getParameter('category'));
  }
  
  public function executeAddcomment(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $comment = new BlogComment();
    $comment->setUserId($this->getUser()->getGuardUser()->getId());
    
    if(sfConfig::get('app_blog_premoderation_comment'))
      $comment->setDisplay('0');
    
    $this->form = new BlogCommentForm($comment);
    
    $this->processComment($request, $this->form);

    $this->post = Doctrine_Core::getTable('BlogPost')->getOnePost($request->getParameter('slug'));
    $categoryUrl = $this->post->getCategoryUrl();
    $this->setTemplate('show');
  }
  
  public function executeAddpage(sfWebRequest $request)
  {
   $this->about = Doctrine_Core::getTable('AddPage')->findOneByPage($request->getParameter('page'));
  }
  
  public function executeError404(){
    $this->error404 = Doctrine_Core::getTable('AddPage')->findOneByPage('error404');
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $blog_post = $form->save();
      $this->redirect('blog/edit?slug='.$blog_post->getName());
    }
  }
  
  protected function processComment(sfWebRequest $request, sfForm $form)
  {
    $this->forward404Unless($post = Doctrine_Core::getTable('BlogPost')->findOneByName($request->getParameter('slug')));
    $form->getObject()->setPostId($post->getId());
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    if ($form->isValid())
    { 
      $blog_comment = $form->save();
      
      if(sfConfig::get('app_blog_premoderation_comment'))
        $message = 'Комментарий добавлен, но к сожалению будет виден только после проверки модератором.';
      else
        $message = 'Комментарий добавлен';
      
      $this->getUser()->setFlash('notice', $message);
      
      $this->redirect('@blog_show?category='.$request->getParameter('category').'&slug='.$request->getParameter('slug').'#id'.$blog_comment->getId());
    }
 
  }
}
